package com.xizi.atomic;

import java.util.concurrent.atomic.AtomicMarkableReference;

public class MyAtomicMarkableReference {
    static AtomicMarkableReference<String> atomicStampedReference = new AtomicMarkableReference("戏子zzz", false);

    public static void main(String[] args) {
        boolean oldMarked = atomicStampedReference.isMarked();
        String oldReference = atomicStampedReference.getReference();

        System.out.println("初始化之后的标记：" + oldMarked); //false
        System.out.println("初始化之后的值：" + oldReference);//戏子zzz

        String newReference = "jerry";

        boolean b = atomicStampedReference.compareAndSet(oldReference, newReference, true, false);
        if (!b) {
            System.out.println("Mark不一致，无法修改Reference的值");
        }
        b = atomicStampedReference.compareAndSet(oldReference, newReference, false, true);
        if (b) {
            System.out.println("Mark一致，修改reference的值为jerry");
        }
        System.out.println("修改成功之后的Mark：" + atomicStampedReference.isMarked());
        System.out.println("修改成功之后的值：" + atomicStampedReference.getReference());
    }
}